<%--
  Created by IntelliJ IDEA.
  User: ASUS
  Date: 2023/5/9
  Time: 15:02
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<style>
    .layui-form-item .layui-input-company {
        width: auto;
        padding-right: 10px;
        line-height: 38px;
    }
</style>
<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">

        <div class="layui-form layuimini-form" lay-filter="pas-filter">
            <div class="layui-form-item">
                <label class="layui-form-label required">旧的密码</label>
                <div class="layui-input-block">
                    <input type="password" name="old_password" lay-verify="required" lay-reqtext="旧的密码不能为空" placeholder="请输入旧的密码" value="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label required">新的密码</label>
                <div class="layui-input-block">
                    <input type="password" name="new_password" lay-verify="required" lay-reqtext="新的密码不能为空" placeholder="请输入新的密码" value="" class="layui-input">
                    <tip>密码长度不小于8位，包含“数字,大写字母,小写字母”中的至少三种~</tip>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">确认密码</label>
                <div class="layui-input-block">
                    <input type="password" name="again_password" lay-verify="required" lay-reqtext="新的密码不能为空" placeholder="请输入新的密码" value="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="pwdChangeBtn">确认保存</button>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    // 密码格式检查
    function checkPassword(pwd) {
        // 格式判断
        var regUpLetter = /[A-Z]/
        var regLowLetter = /[a-z]/
        var regNumber = /[0-9]/

        var complex = 0;
        if (regUpLetter.test(pwd)) {
            ++complex;
        }
        if (regLowLetter.test(pwd)) {
            ++complex;
        }
        if (regNumber.test(pwd)) {
            ++complex;
        }
        return !(complex < 3 || pwd.length < 8);
    }
    layui.use(['form', 'miniPage'], function () {
        var form = layui.form,
            layer = layui.layer,
            miniPage = layui.miniPage;
        var $ = layui.$;

        var user='${loginUser}';
        user=JSON.parse(user);
        form.val('pas-filter',{
            "old_password":user.password
        })

        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        form.on('submit(pwdChangeBtn)', function (data) {
            var password1 = data.field.new_password,
                password2 = data.field.again_password;
            if(data.field.old_password !== user.password){
                layer.msg("原始密码错误")
                return false;
            }
            if(password1 === password2 && checkPassword(password1)){
                user.password = password1;
                var index = layer.alert("是否确认修改密码", {
                    title: '修改信息'
                }, function () {
                    $.ajax({
                        url: "/user/updateDo",
                        data: JSON.stringify(user),
                        contentType: "application/json;charset=UTF-8",
                        dataType: "json",
                        type: "POST",
                        success: function (res) {
                            if (res.status === 200) {
                                layer.msg(res.message, {icon: 1, time: 1000});
                                layer.close(index);
                                // parent.layer.close(parentIndex);
                            } else {
                                layer.msg(res.message, {icon: 0, time: 1000});
                                layer.close(index);
                            }
                        },
                        error: function (e) {
                            console.log(e, e.message);
                            alert('error');
                        }
                    });
                })
            }else{
                layer.msg("请输入正确格式的密码并进行二次确认", {icon: 0, time: 1000});
            }
            return false;
        });

    });
</script>
